namespace DigitalCv.Desktop.ViewModels
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;

    using Caliburn.Micro;

    using DigitalCv.Data;

    public sealed class SynchronizedBindableCollection<TModel> : BindableCollection<TModel>        
    {
        private readonly ICollection<TModel> collection;
        
        public SynchronizedBindableCollection(ICollection<TModel> collection)
        {
            this.collection = collection;
            this.AddRange(collection);         
            this.CollectionChanged += ObservedCollectionOnCollectionChanged;               
        }

        private void ObservedCollectionOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
        {
            switch (notifyCollectionChangedEventArgs.Action)
            {
                case NotifyCollectionChangedAction.Add:                    
                    notifyCollectionChangedEventArgs.NewItems.OfType<TModel>().ToList().ForEach(i => this.collection.Add(i));                     
                    break;
                case NotifyCollectionChangedAction.Remove:
                    notifyCollectionChangedEventArgs.OldItems.OfType<TModel>().ToList().ForEach(i => this.collection.Remove(i));                    
                    break;                
                case NotifyCollectionChangedAction.Reset:
                    //this.collection.Clear();
                    break;
            }
        }
    }    

    public class TechnologyViewModel : Screen
    {        
        public IObservableCollection<Technology> Technologies { get; set; }       

        public bool IsEditing { get; set; }

        public TechnologyViewModel(ICollection<Technology> technologies)
        {
            this.Technologies = new SynchronizedBindableCollection<Technology>(technologies);            
        }

        public Technology SelectedTechnology { get; set; }

        public void Add()
        {
            this.Technologies.Add(new Technology());
        }

        public void Edit(Technology technology)
        {
            if (technology == null)
            { 
                return;                
            }


        }

        public void Delete(Technology technology)
        {
            if (technology == null)
            {
                return;
            }

            if (this.Technologies.Contains(technology))
            {
                this.Technologies.Remove(technology);                    
            }
        }
    }
}